#include "stdafx.h"
#include "MPluginHost.h"

#include "ExplorerFrm.h"
#include "DialogFileCopy.h"

CMPluginHost::CMPluginHost(void)
{
}

CMPluginHost::~CMPluginHost(void)
{
}

HRESULT CMPluginHost::Initialize()
{
	m_PluginHostInfo.version = 0x10101000;
	m_PluginHostInfo.description = _T("Explorer");

	return S_OK;
}

LPPluginHostInfo CMPluginHost::GetInfo()
{
	return &m_PluginHostInfo;
}

LPCTSTR CMPluginHost::ShowCopyAsDlg(const std::vector<CString>& vecFiles, bool bIsCopyTo)
{
	CDialogFileCopy(vecFiles,
					std::vector<CString>(), 
					CExplorerFrm::m_strPrevFolderPath, 
					_T(""),
					bIsCopyTo,
					false).DoModal();

	return CDialogFileCopy::m_strSelectPath;
}

HRESULT CMPluginHost::GetDirectory(LPTSTR* ppszDirectory, uint16 nCount, EDirectoryType eDirectoryType)
{
	if(ppszDirectory == NULL || nCount == 0)
		return E_INVALIDARG;

	switch(eDirectoryType)
	{
	case DT_Prev:
		_tcscpy(ppszDirectory[0], CExplorerFrm::m_strPrevFolderPath);
		break;
	}

	return S_OK;
}

HRESULT CMPluginHost::RegisterCopyAsPath(LPCTSTR lpszDesc, ProcessFun Process, void* pUserData)
{
	if(lpszDesc == NULL || Process == NULL)
		return E_INVALIDARG;

	m_mapProcessCopyAsFun[lpszDesc] = std::make_pair(Process, pUserData);
	return S_OK;
}

HRESULT CMPluginHost::UnregisterCopyAsPath(LPCTSTR lpszDesc)
{
	if(lpszDesc == NULL)
		return E_INVALIDARG;

	m_mapProcessCopyAsFun.erase(lpszDesc);
	return S_OK;
}

void CMPluginHost::GetCopyAsPaths(std::vector<CString>& vecCopyAsPaths)
{
	MapProcessFunInfo::iterator ite = m_mapProcessCopyAsFun.begin();
	for(; ite != m_mapProcessCopyAsFun.end(); ++ite)
	{
		vecCopyAsPaths.push_back((*ite).first);
	}
}

void CMPluginHost::ProcessCopyAs(const CString& strDesc, const std::vector<CString>& vecFilePaths)
{
	MapProcessFunInfo::iterator ite = m_mapProcessCopyAsFun.find(strDesc);
	if(ite == m_mapProcessCopyAsFun.end())
		return;

	(*ite).second.first((*ite).second.second, vecFilePaths);
}
